package net.wanho.day03;
import net.wanho.dao.CourseDaoI;
import net.wanho.dao.StudentDaoI;
import net.wanho.entity.Course;
import net.wanho.entity.Student;
import net.wanho.vo.CourseInfo;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


public class CourseTest {
    static String config = "mybatisConfig.xml";
    static SqlSession ss = null;

    static {
        InputStream in = null;
        try {
            in = Resources.getResourceAsStream(config);
        } catch (IOException e) {
            e.printStackTrace();
        }
        SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(in);
        ss = ssf.openSession();
    }

    public static void main(String[] args) throws SQLException {
//        insert();
//        insertCourse();
//        selectManyCon();
//        selectByYearRange();
//        updateCourse();
//        selectByCidSet();
        selectByCidArray();
    }
    public static void selectYear(){
        CourseDaoI courseDao=ss.getMapper(CourseDaoI.class);
        List<Course>list=courseDao.selectByYear(2016);
        System.out.println(list);
    }
    public static void insert() throws SQLException {
        CourseDaoI courseDao=ss.getMapper(CourseDaoI.class);
        courseDao.insert("asdad","dwad",2010,"802");
        ss.getConnection().setAutoCommit(true);
    }
    public static void insertCourse()throws SQLException{
        CourseDaoI courseDao=ss.getMapper(CourseDaoI.class);
        Course course =new Course();
        course.setCname("aaa");
        course.setCreateYear(2343);
        course.setProfession("dad");
        course.setTno("d2qdqw");
        courseDao.insertCourse(course);
        System.out.println(course);
    }
    public static void selectManyCon(){
        CourseDaoI courseDao=ss.getMapper(CourseDaoI.class);
        Course course =new Course();
        course.setCid(7);
        List<Course>list=courseDao.selectManyCon(course);
        System.out.println(list);
    }
    public static void selectByYearRange(){
        CourseDaoI courseDao=ss.getMapper(CourseDaoI.class);
        Course course =new Course();
        course.setCreateYear(2015);
        List<Course> list=courseDao.selectByYearRange(course);
        System.out.println(list);
    }
    public static void updateCourse() throws SQLException {
        ss.getConnection().setAutoCommit(true);
        CourseDaoI courseDao=ss.getMapper(CourseDaoI.class);
        Course course =new Course();
        course.setCid(8);
        course.setCreateYear(3800);
        course.setProfession("心理学111");
        courseDao.updateCourse(course);
    }
    public static void selectByCidSet(){
        CourseDaoI courseDao=ss.getMapper(CourseDaoI.class);
        List<Integer>list=new ArrayList<Integer>();
        list.add(3);
        list.add(4);
        list.add(5);
        List<Course>list1=courseDao.selectByCidSet(list);
        System.out.println(list1);
    }

    public static void selectByCidArray(){
        CourseDaoI courseDao=ss.getMapper(CourseDaoI.class);
        Integer [] ints=new Integer[]{1,2,3,4};
        List<Course>list=courseDao.selectByCidArray(ints);
        System.out.println(list);
    }

    public static void selectByCourseInfo(){
        CourseDaoI courseDao=ss.getMapper(CourseDaoI.class);
        CourseInfo courseInfo=new CourseInfo();
        List<Integer> ints = new ArrayList<Integer>();
        ints.add(2);
        ints.add(3);
        courseInfo.setCids(ints);
        List<Course> courses = courseDao.selectByCourseInfo(courseInfo);
        System.out.println(courses);
    }
}
